package com.yy.yyui.button
{
	import com.yy.yyui.IDataContainer;
	
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.StaticText;
	import flash.text.TextField;

	public class SwitchButton extends SimpleButton implements IDataContainer
	{
		private var _icon:DisplayObject
		private var _data:Object;
		private var _selected:Boolean;
		private var _enabled:Boolean;
		private var selectedFilters:Array;
		private var hoverFilters:Array;
		private var oldFilters:Array;
		
		private var _button:SimpleButton;
		public function SwitchButton(button:SimpleButton,selectedFilters:Array = null,hoverFilters:Array=null,defaultFilters:Array = null)
		{
			button.hitTestState = button.upState;
			super(button.upState, 
			button.overState,
			button.downState,
			button.hitTestState);
			this._button = button;	
			this.tabEnabled = false;
			this.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		public function get button():SimpleButton
		{
			return _button;
		}
		
		private function clickHandler(e:MouseEvent):void{
			if(this.onSelect!=null)
			{
				this.onSelect(e);
			}
		}
		
		private var _onSelect:Function;
		public function get onSelect():Function{return this._onSelect;}
		
		public function set onSelect(value:Function):void
		{
			this._onSelect = value;
		}
		
		public function get selected():Boolean{return this._selected;}
		
		public function set selected(value:Boolean):void
		{
			if(value != this.selected)
			{
				if(value)
				{
					this._button.upState.filters = null;
				}else
				{
					this._button.upState.filters = null;
				}
			}
			this._selected = value;
		}
		private var _cTextColor:uint;
		override public function set enabled(value:Boolean):void{
			this._enabled = value;
			this.mouseEnabled = value;
			this.useHandCursor = value;
			var tF:TextField = getTextField();
			if(value){
				tF.textColor = _cTextColor;
			}else{
				_cTextColor = tF.textColor; 
				tF.textColor = 0x2F2F2F;
			}
		}
		private function getTextField():TextField
		{
			var sp:Sprite = this.upState as Sprite;
			for(var i:int = 0 ; i < sp.numChildren ; i++)
			{
				var obj:Object = sp.getChildAt(i);
				if(obj is TextField)
				{
					return obj as TextField;
				}
			}
			return null;
		}
		
		override public function get enabled():Boolean{
			return this._enabled;
		}
		
		public function get data():Object{return _data;}
		public function set data(value:Object):void{this._data = value;}
					
	}
}